package com.tomise.demo.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tomise.core.controller.BaseController;
import com.tomise.core.page.Page;
import com.tomise.demo.bean.FootBean;
import com.tomise.demo.service.FootService;
@Controller
@RequestMapping(value = "/demo/foot")
public class FootController extends BaseController{
	@Autowired
	private FootService footService;
	
	@RequestMapping(value = "/add")
	public String add(HttpServletResponse arg0) {
		
		this.setAttr("root", this.request.getContextPath());
		return "demo/foot/add";
	}
	@RequestMapping(value = "/edit")
	public String edit(HttpServletResponse arg0) {
		String id = this.getPara("id");
		FootBean foot = this.footService.getFootBeanById(id);
		this.setAttr("foot", foot);
		this.setAttr("root", this.request.getContextPath());
		return "demo/foot/edit";
	}
	@RequestMapping(params="method=insert",method=RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletResponse response,FootBean bean) {
		try{
			this.footService.saveFootBean(bean);
			this.ajaxDoneSuccess(response, null);
		}catch(Exception e){
			this.ajaxDoneError(response, null);
		}
	}
	@RequestMapping(params="method=update",method=RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletResponse response,FootBean bean) {
		try{
			bean.setId(this.getPara("id"));
			this.footService.updateFootBean(bean);
			this.ajaxDoneSuccess(response, null);
		}catch(Exception e){
			this.ajaxDoneError(response, null);
		}
	}
	@RequestMapping(params="method=delete",method=RequestMethod.POST)
	@ResponseBody
	public void delete(HttpServletResponse response) {
		String id = this.getParaJsonStr("id");
		String[] code=id.split(",");
		List<Object> list=new ArrayList<Object>();
		try{
			if(code.length>1){//批量删除操作
			for(int i=0;i<code.length;i++){
				list.add(code[i]);
			}
			this.footService.batchDelete(list);
		}else{//单个删除操作
			this.footService.delete(id);
		}
			this.ajaxDoneSuccess(response, null);
		}catch(Exception e){
			e.printStackTrace();
			this.ajaxDoneError(response, null);
		}
	}
	
	@Override
	@RequestMapping(value = "/index")
	public String index(HttpServletResponse arg0, ModelMap arg1) {
		this.setAttr("root", this.request.getContextPath());
		return "demo/foot/index";
	}
	@Override
	@RequestMapping(value = "/query")
	public void query(@RequestParam Integer page, @RequestParam Integer rows,
			HttpServletRequest request, HttpServletResponse response, ModelMap map){
		try {
			String select ="select * ";
			String tableWhere = "from demo_foot ";
			String orderBy = "order by FOOT_TIME desc";
			List<Object> sqlParam = new ArrayList<Object>();
			/**
			 * {total: 3, page: 3, records: 21, 
			 * rows: {['1211','1233-09-01','123.0','123.0','123.0','xxp']}}
			 */
			Page footPage = this.footService.page(page,rows,select,tableWhere,orderBy,sqlParam.toArray());
			com.alibaba.fastjson.JSONObject jsonObject = toJQGridPage(footPage);
			this.renderJson(response, jsonObject.toJSONString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}

